﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace WinFormsDictionary
{
    public partial class Form1 : Form
    {
        string e;
        string c;
        int count = 0;
        public Form1()
        {
            InitializeComponent();
            //dataGridViewDictionary.DataSource =  bindingSource1;



            /*
            SqlConnection sqlConnection = new SqlConnection(conString);
            sqlConnection.Open();
            try
            {
                sqlConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            string sql = "select * from EnWords";
            SqlCommand cmd = new SqlCommand();//创建SQL语句处理对象
                                              //或SqlCommand cmd = new SqlCommand(sql,sqlConnection);//构造函数绑定
            cmd.Connection = sqlConnection;//绑定连接
            cmd.CommandText = sql;//绑定SQL语句
            int count = (int)cmd.ExecuteScalar();
            */
            /*
            OleDbConnection myconnection = new OleDbConnection(conString);
            OleDbDataAdapter daAdapter = new OleDbDataAdapter(strsql, myconnection);
            OleDbCommandBuilder cmdbld = new OleDbCommandBuilder(daAdapter);
            DataSet dsMyData = new DataSet();
            daAdapter.Fill(dsMyData);
            dataGridViewDictionary.DataSource = dsMyData.Tables[0];
            */
            using (MySqlConnection conn = GetConnection())
            {
                string strsql = "SELECT * FROM enwords";
                using (MySqlCommand cmd = new MySqlCommand(strsql, conn))
                {
                    DataSet dsMyData = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dsMyData);
                    dataGridViewDictionary.DataSource = dsMyData.Tables[0];
                    //dataGridViewDictionary.DataSource = bindingSource1;
                    DataTable table = dsMyData.Tables[0];


                }
            }
        }
        private static MySqlConnection GetConnection()
        {
            string conString = "data source=localhost;initial catalog=test;user id=root;password=123456";
            MySqlConnection connection = new MySqlConnection(conString);
            connection.Open();
            return connection;
        }

        /*
        private void ShowDictionary(string e, string c)
        {
            var pageInfo = new { Index = bindingSource1.Count + 1, English = e, Chinese = c };
            Action action = () => { bindingSource1.Add(pageInfo); };
            if (this.InvokeRequired)
            {
                this.Invoke(action);
            }
            else
            {
                action();
            }
        }
        */
        private void button1_Click(object sender, EventArgs e)
        {
            
            using (MySqlConnection conn = GetConnection())
            {
                string strsql = "SELECT * FROM enwords";
                using (MySqlCommand cmd = new MySqlCommand(strsql, conn))
                {
                    DataSet dsMyData = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dsMyData);
                    dataGridViewDictionary.DataSource = dsMyData.Tables[0];
                    //dataGridViewDictionary.DataSource = bindingSource1;
                    DataTable table = dsMyData.Tables[0];
                    /*
                    foreach (DataRow row in table.Rows)
                    {
                        bool over = false;
                        while (!over)
                        {
                            label3.Text = row.ItemArray[1].ToString();
                            string answer = textBox1.Text;
                            if (answer == row.ItemArray[0].ToString())
                            {
                                label4.Text = "正确";
                                over = true;
                            }
                            else
                            {
                                label4.Text = "错误";
                            }
                        }
                    

                    }
                    */
                    if (count < table.Rows.Count)
                    {
                        label3.Text = table.Rows[count].ItemArray[1].ToString();
                    }
                    else
                    {
                        count = 0;
                        label3.Text = table.Rows[0].ItemArray[1].ToString();
                    }
                }
                
            }
            count++;
            label4.Text = "错误";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            using (MySqlConnection conn = GetConnection())
            {
                string strsql = "SELECT * FROM enwords";
                using (MySqlCommand cmd = new MySqlCommand(strsql, conn))
                {
                    DataSet dsMyData = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dsMyData);
                    dataGridViewDictionary.DataSource = dsMyData.Tables[0];
                    DataTable table = dsMyData.Tables[0];
         
                    
                    string answer = textBox1.Text;
                    if (answer == table.Rows[count-1].ItemArray[0].ToString())
                    {
                        label4.Text = "正确";

                    }
                    else
                    {
                        label4.Text = "错误";
                    }
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            count = 0;
        }
    }
    

}


